local logger = require "utils.logger"
local movement = require "modules.movement"
local tslib = require "TSLib"

-- 添加一个变量标记当前喝药状态是中药还是小药，默认是小药
local currentHeYaoState = false

-- 设置喝中药
local function setHeyao()
    if not multiColor({ { 838, 171, 0x7a0818 }, { 851, 169, 0xd61221 }, { 884, 166, 0x494ce7 } }) then
        tap(1324, 63)
        mSleep(500)
    end
    -- 挂机页面点击设置
    local buttons = { { 868, 163, 0xdf444c }, { 187, 275, 0x191919 }, { 827, 16, 0x6ae0e7 } }
    movement.clickSeries(buttons)
    currentHeYaoState = true
end

-- 设置喝小药
local function setHeXiaoYao()
    if not multiColor({ { 838, 171, 0x7a0818 }, { 851, 169, 0xd61221 }, { 884, 166, 0x494ce7 } }) then
        tap(1324, 63)
        mSleep(500)
    end
    -- 挂机页面点击设置
    local buttons = { { 863, 158, 0xf3b0a7 }, { 186, 222, 0xb0b387 }, { 1024, 520, 0xffffff }, { 826, 20, 0x6cdde1 } }
    movement.clickSeries(buttons)
    currentHeYaoState = false
end

local function main()
    while true do
        logger.info("监测血条")
        if multiColor({{748, 12, 0x98e5e8},{750, 16, 0x32d5db},{752, 20, 0x08b1b8},{757, 20, 0x59d9de}}) then
            tap(748, 12)
        end
        -- 是否打开客服，关闭客服
        if multiColor({{809, 27, 0xffe11f},{809, 39, 0xffe11f},{809, 44, 0xffe11f},{807, 54, 0xfee01f}}) then
            tap(44, 42)
        end
        if not multiColor({{514, 780, 0x800501},{515, 780, 0x840601},{518, 780, 0x920801},{521, 780, 0xa40a01}}) then
            logger.info("血条降到一半切换喝中药")
            setHeyao()
            mSleep(20 * 1000)
        elseif currentHeYaoState then
            logger.info("血条升到一半切换喝小药")
            setHeXiaoYao()
        end
        mSleep(3000)
    end
end


return {
    run = main
}
